package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.AdminInfo;
import com.dao.AdminDao;
import com.utils.DESUtil;

/**
 * 登录系统
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private AdminDao _adminDao=new AdminDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String adminName=request.getParameter("adminName");
		String password=request.getParameter("password");
		
		AdminInfo admin=_adminDao.login(adminName,DESUtil.encrypt(password));
		if(admin!=null) {
			if(admin.getState().equals("1")) {
				request.getSession().setAttribute("session_admin", admin);
				request.getRequestDispatcher("/main.jsp").forward(request, response);
			}else if(admin.getState().equals("2")) {
				request.setAttribute("msg", "2");
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}else {
				request.setAttribute("msg", "0");
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		}
		else {
			request.setAttribute("msg", "0");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
